package com.bocloud.cmp.driver.providers.ali;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.BucketInfo;
import com.aliyun.oss.model.CannedAccessControlList;
import com.bocloud.cmp.driver.providers.BucketDriver;
import com.bocloud.cmp.model.BucketModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;

/**
 * 阿里云Bucket相关方法实现
 * 
 * @author dmw
 *
 */
public class AliBucketDriver extends AliDriver implements BucketDriver {

	private static Logger logger = LoggerFactory.getLogger(AliBucketDriver.class);
	private OSSClient ossClient;

	/**
	 * 
	 */
	public AliBucketDriver() {
		super();
	}

	/**
	 * @param accessKey
	 * @param secretKey
	 * @param regionId
	 */
	public AliBucketDriver(String accessKey, String secretKey, String endpoint) {
		this.ossClient = new OSSClient(endpoint, accessKey, secretKey);
	}

	@Override
	public void destory() {
		if (null != ossClient) {
			ossClient.shutdown();
		}
	}

	@Override
	public BsmResult create(String bucketName, boolean share) {
		try {
			boolean exist = ossClient.doesBucketExist(bucketName);
			if (exist) {
				logger.warn("bucket [{}] 已经存在！", bucketName);
				return new BsmResult(false, null, "Bucket 已经存在！");
			}
			Bucket bucket = ossClient.createBucket(bucketName);
			if (share) {
				ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
			} else {
				ossClient.setBucketAcl(bucketName, CannedAccessControlList.Private);
			}
			return new BsmResult(true, detail(bucket.getName()).getData(), "success");
		} catch (OSSException | ClientException e) {
			logger.error("create bucket [{}] error:", bucketName, e);
			return new BsmResult(false, e.getMessage(), "创建Bucket失败");
		} finally {
			this.destory();
		}
	}

	@Override
	public BsmResult remove(String bucket) {
		try {
			ossClient.deleteBucket(bucket);
			return new BsmResult(true, "bucket 删除中");
		} catch (OSSException | ClientException e) {
			logger.error("bucket [{}] remove error:", bucket, e);
			return new BsmResult(false, e.getMessage(), "Bucket 删除失败！");
		} finally {
			this.destory();
		}
	}

	@Override
	public BsmResult exists(String bucket) {
		try {
			boolean exist = ossClient.doesBucketExist(bucket);
			return new BsmResult(exist, "bucket exist:" + exist);
		} catch (OSSException | ClientException e) {
			logger.error("bucket [{}] exist error:", bucket, e);
			return new BsmResult(false, e.getMessage(), "获取Bucket信息失败");
		} finally {
			this.destory();
		}
	}

	@Override
	public BsmResult detail(String bucket) {
		try {
			BucketInfo bucketInfo = ossClient.getBucketInfo(bucket);
			if (null == bucketInfo) {
				return new BsmResult(false, null, "Bucket [" + bucket + "]不存在！");
			}
			return new BsmResult(true, convert(bucketInfo.getBucket()), "success");
		} catch (OSSException | ClientException e) {
			logger.error("bucket [{}] detail error:", bucket, e);
			return new BsmResult(false, e.getMessage(), "获取失败！");
		} finally {
			this.destory();
		}
	}

	@Override
	public BsmResult list() {
		try {
			List<BucketModel> models = Lists.newArrayList();
			List<Bucket> buckets = ossClient.listBuckets();
			for (Bucket bucket : buckets) {
				models.add(convert(bucket));
			}
			return new BsmResult(true, models, "success");
		} catch (OSSException | ClientException e) {
			logger.error("list Bucket error:", e);
			return new BsmResult(false, e.getMessage(), "获取Bucket 异常！");
		} finally {
			this.destory();
		}
	}

	@Override
	public BsmResult acl(String bucket, boolean share) {
		try {
			BucketInfo bucketInfo = ossClient.getBucketInfo(bucket);
			if (null == bucketInfo) {
				return new BsmResult(false, null, "Bucket [" + bucket + "]不存在！");
			}
			if (share) {
				ossClient.setBucketAcl(bucket, CannedAccessControlList.PublicRead);
			} else {
				ossClient.setBucketAcl(bucket, CannedAccessControlList.Private);
			}
			return new BsmResult(true, bucket, "success");
		} catch (OSSException | ClientException e) {
			logger.error("bucket [{}] detail error:", bucket, e);
			return new BsmResult(false, e.getMessage(), "授权失败！");
		} finally {
			this.destory();
		}
	}

	private BucketModel convert(Bucket bucket) {
		BucketModel bucketModel = new BucketModel();
		bucketModel.setGmtCreate(bucket.getCreationDate());
		bucketModel.setName(bucket.getName());
		bucketModel.setSpace(0.0);
		String location = bucket.getLocation();
		bucketModel.setLocation(location);
		String regionId = location.substring(4);
		bucketModel.setRegionId(regionId);
		return bucketModel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bocloud.emaas.butler.plugins.ali.AliDriver#accessable()
	 */
	@Override
	public boolean accessable() {
		// TODO Auto-generated method stub
		return super.accessable();
	}

}
